package com.example.paimengshop;

import com.example.paimengshop.client_socket.ActionControl_Send;
import com.example.paimengshop.mypanes.CartPane;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class collectionController implements Initializable {
    @FXML
    private AnchorPane anchorPane;
    @FXML
    private ImageView orderTitle;

    @FXML
    public VBox vBox;

    @FXML
    private RadioButton chooseAllButton;

    @FXML
    private Button deleteButton;

    private ArrayList<CartPane> cartPanes = new ArrayList<>();

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle)
    {

        anchorPane.getStylesheets().add(getClass().getResource("All.css").toExternalForm());

        chooseAllButton.getStyleClass().add("radio-button");
        deleteButton.getStyleClass().add("btn-basic");

        //控件样式
        orderTitle.setImage(new Image("file:PaiMengShop/PaiMengShopClient/src/main/resources/com/example/paimengshop/beautifyImage/collectionTitle.png"));


        vBox.getChildren().clear();
        cartPanes.clear();
        vBox.setPrefHeight(Data.currentUser.getCollection().size()*200);
        vBox.setAlignment(Pos.CENTER);

        for(int i=0;i<Data.currentUser.getCollection().size();i++){
            CartPane cartPane=new CartPane(Data.currentUser.getCollection().get(i));
            vBox.getChildren().add(cartPane);
            cartPanes.add(cartPane);

            cartPane.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {
                    anchorPane.getChildren().clear();
                    anchorPane.getChildren().add(homeController.getCommodityDetail(cartPane.getCommodity()));
                }
            });
        }


        deleteButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent actionEvent)
            {
                for (int i = 0; i < cartPanes.size(); i++)
                {
                    if (cartPanes.get(i).getCheckBox().isSelected())
                    {
                        Data.currentUser.getCollection().remove(cartPanes.get(i).getCommodity());
                    }
                }

                main.getSend().Send(ActionControl_Send.UpdateCollection(Data.currentUser, Data.currentUser.getCollection()));

                vBox.getChildren().clear();
                vBox.setPrefHeight(Data.currentUser.getCollection().size() * 200);
                vBox.setAlignment(Pos.CENTER);

                for (int i = 0; i < Data.currentUser.getCollection().size(); i++)
                {
                    CartPane cartPane = new CartPane(Data.currentUser.getCollection().get(i));
                    vBox.getChildren().add(cartPane);
                    cartPanes.add(cartPane);

                    cartPane.setOnMouseClicked(new EventHandler<MouseEvent>()
                    {
                        @Override
                        public void handle(MouseEvent mouseEvent)
                        {
                            anchorPane.getChildren().clear();
                            anchorPane.getChildren().add(homeController.getCommodityDetail(cartPane.getCommodity()));
                        }
                    });
                }
            }
        });

        chooseAllButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent actionEvent)
            {
                if(chooseAllButton.isSelected())
                {
                    for (int i = 0; i < Data.currentUser.getCollection().size(); i++)
                    {
                        cartPanes.get(i).getCheckBox().setSelected(true);
                    }
                }
                else
                {
                    for (int i = 0; i < Data.currentUser.getCollection().size(); i++)
                    {
                        cartPanes.get(i).getCheckBox().setSelected(false);
                    }
                }
            }
        });
    }
}
